Set Field Values

Initialize New Record from Existing Record

Description
This example demonstrates how to initialize values in a Page, based on values from an existing record, essentially implementing a copy record function. This example assumes you are passing the primary key value of the existing record as a URL parameter.
Variables
Table Name
Select the name of the database table
Primary Key
Select Primary key field in the selected database table
Initialization Field
Select the database field that corresponds to the control that needs to be initialized.
Initialization Field Control
Select the control that needs to be initialized.
Record Control Class
Select the record control where this customization will be inserted
Applies to
RecordControl class
Code
 
/// 
/// Override DataBind(),call base Databind() to populate the UI
/// controls using the DataSource and then initilize the values of
/// field control
/// 
public override void DataBind()
{
    // Call base.DataBind()
    base.DataBind();
    if (!this.Page.IsPostBack)
    {
        // Get the key from the URL.
        string keyFromUrl = this.Page.Request.QueryString["Initfrom${Table Name}"];

        // keyFromUrl variable will contain a parameter string as follows
        // InitfromEmployees=4 or InitfromEmployees=<...xml parameter definition...>
        // set this url for New button in a page with table control.
        if (keyFromUrl != null && (keyFromUrl.Trim().Length > 0)) 
        {
            // Create the where clause.
            WhereClause wc = new WhereClause();
            string recId = keyFromUrl;
            if (KeyValue.IsXmlKey(recId))
            {
                // Key are typically passed as XML structures to handle composite keys.
                // If XML, then add a Where clause based on the Primary Key in the XML.
                KeyValue pkValue = KeyValue.XmlToKey(recId);
                wc.iAND(${${Table Name}ClassName}.${Primary Key}, BaseFilter.ComparisonOperator.EqualsTo, pkValue.GetColumnValue(${${Table Name}ClassName}.${Primary Key}).ToString());
            }
            else
            {
                // The URL parameter contains the actual value, not an XML structure.
                wc.iAND(${${Table Name}ClassName}.${Primary Key}, BaseFilter.ComparisonOperator.EqualsTo, recId);
            }

            // Get existing record based on primary key.
            ${${Table Name}RecordClassName}[] rec  = ${${Table Name}ClassName}.GetRecords(wc, null, 0, 2);
            if (rec.Length > 0) 
            {
                // Populate each field value based on existing record's values.
                this.${Initialization Field Control}.Text = rec[0].${Initialization Field}.ToString();        
            }
        }
    }
}

     

Terms of Service Privacy Statement